package cn.edu.scujj.view;
import cn.edu.scujj.bean.BookType;
import cn.edu.scujj.dao.BookTypeDao;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.Vector;
import java.util.List;
public class BookTypeUI extends JFrame {
    JScrollPane jScrollPane = new JScrollPane();
    AbstractTableModel tableModel ;
    Vector vector = new Vector();
    JTextField textField1 = new JTextField(4);//添加
    JTextField textField2 = new JTextField(4);//修改
    BookTypeDao dao = new BookTypeDao();
    public BookTypeUI() {
        setTitle("图书类别管理");
        setBounds(100,100,800,600);
        setLayout(new BorderLayout());

        JPanel panelUP= new JPanel();
        JPanel panelDown = new JPanel();
        JPanel panelCenter = new JPanel();

        panelUP.add(jScrollPane);
        createTable();

        add(panelUP,BorderLayout.NORTH);
        add(panelCenter,BorderLayout.CENTER);
        add(panelDown,BorderLayout.SOUTH);


        panelCenter.add(new JLabel("id"));
        panelCenter.add(textField1);
        panelCenter.add(new JLabel("类名"));
        panelCenter.add(textField2);
        panelCenter.add(Box.createVerticalStrut(100)); // 添加换行
        JButton btnAdd = new JButton("添加");
        btnAdd.addActionListener(e -> addDate());
        JButton btnchax = new JButton("查询");
        btnchax.addActionListener(e -> query());
        JButton btnXiugai = new JButton("修改");
        btnXiugai.addActionListener(e -> update());
        JButton btnDelete = new JButton("删除");
        btnDelete.addActionListener(e -> delete());

        panelDown.add(btnAdd);
        panelDown.add(btnchax);
        panelDown.add(btnXiugai);
        panelDown.add(btnDelete);


        select();
    }



    public void addDate() {
            BookTypeDao dao = new BookTypeDao();
            BookType bookType = new BookType();
            bookType.setTypeId(Integer.parseInt(textField1.getText())); // 使用textField1作为ID输入
            bookType.setTypeName(textField2.getText()); // 使用textField2作为名称输入
        if(dao.addBookType(bookType)){
                JOptionPane.showMessageDialog(this,"添加成功");
                select();
                textField1.setText("");
                textField2.setText("");
            }else {
                JOptionPane.showMessageDialog(this, "添加失败，id不能重复");
                textField1.setText("");
                textField2.setText("");
            }
        }

    public void query() {
        BookTypeDao dao = new BookTypeDao();
        String id = textField1.getText().trim();
        String name = textField2.getText().trim();

        if(id.isEmpty() && name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入ID或类名");
            return;
        }

        List<BookType> bookTypes;
        if(!id.isEmpty()) {
            try {
                bookTypes = dao.queryBookTypes(id, true, new BookType());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "ID必须为数字");
                return;
            }
        } else {
            bookTypes = dao.queryBookTypes(name, false, new BookType());
        }
        vector.clear();
        for (BookType bookType : bookTypes) {
            Vector<Object> v = new Vector<>();
            v.add(bookType.getTypeId());
            v.add(bookType.getTypeName());
            vector.add(v);
        }

        if (vector.size() > 0) {
            String queryType = !id.isEmpty() ? "按ID" : "按类名";
            JOptionPane.showMessageDialog(this, queryType + "查询成功，共" + vector.size() + "条记录");
        } else {
            JOptionPane.showMessageDialog(this, "没有查询到结果");
        }

        tableModel.fireTableDataChanged();
        textField1.setText("");
        textField2.setText("");
    }

    public void update() {
        BookTypeDao dao = new BookTypeDao();
        BookType bookType = new BookType();
        bookType.setTypeId(Integer.parseInt(textField1.getText()));
        bookType.setTypeName(textField2.getText()); // 使用textField2作为名称输入
        if(dao.updateBookType(bookType)){
            JOptionPane.showMessageDialog(this,"修改成功");
            select();
        }else {
            JOptionPane.showMessageDialog(this, "修改失败");
        }
    }
    public void delete() {
        BookTypeDao dao = new BookTypeDao();
        BookType bookType = new BookType();
        bookType.setTypeId(Integer.parseInt(textField1.getText())); // 使用textField1作为ID输入
        if (dao.deleteBookType(bookType)){
            JOptionPane.showMessageDialog(this, "删除成功");
            select();
        }
        else {
            JOptionPane.showMessageDialog(this, "删除失败，有关联书籍");
        }
    }
    private void createTable() {
        String[] columnNames = {"图书类名id","图书类名"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);
        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return ((Vector)vector.get(rowIndex)).get(columnIndex);
            }

            @Override
          public String getColumnName(int column) {
                return columnNames[column];
            }
        };
        table.setModel(tableModel);
    }

    void select() {
        vector.clear();
        BookTypeDao dao = new BookTypeDao();
        List<BookType> list = dao.findAll();
        for (BookType bookType : list) {
            Vector v = new Vector();
            v.add(bookType.getTypeId());
            v.add(bookType.getTypeName());
            vector.add(v);
        }
        tableModel.fireTableDataChanged();
    }

    public static void main(String[] args) {
        new BookTypeUI().setVisible(true);
    }

}

